package com.android.pocketguru.Fragments;


import com.android.pocketguru.Constants;
import com.android.pocketguru.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TimePicker;

public class SettingsFragment extends Fragment{

	private int settings_h1;
	private int settings_m1;
	private int settings_h2;
	private int settings_m2;
	private int setting_frequency;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

    	// Loading saved state
        //if (savedInstanceState != null) { }
    	
        // Loading settings
        SharedPreferences prefs = getActivity().getSharedPreferences(Constants.SETTING_PREFERENCE_NAME_KEY, 0);
        
        // time
        settings_h1= prefs.getInt(Constants.SETTING_HOURS_FROM_KEY, Constants.SETTING_HOURS_FROM_DEFAULT);
        settings_m1= prefs.getInt(Constants.SETTING_MINUTES_FROM_KEY,  Constants.SETTING_MINUTES_FROM_DEFAULT);
        settings_h2= prefs.getInt(Constants.SETTING_HOURS_TO_KEY,  Constants.SETTING_HOURS_TO_DEFAULT);
        settings_m2= prefs.getInt(Constants.SETTING_MINUTES_TO_KEY,  Constants.SETTING_MINUTES_TO_DEFAULT);
        
        // frequency
        setting_frequency = prefs.getInt(Constants.SETTING_FREQUENCY_KEY, Constants.SETTING_FREQUENCY_DEFAULT);
        
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.settings, container, false);
    }
    
    @Override
    public void onStart() {
        super.onStart();

    	TimePicker timepicker1 = (TimePicker) getActivity().findViewById(R.id.tp_time1);
    	timepicker1.setIs24HourView(true);
    	timepicker1.setCurrentHour(settings_h1);
    	timepicker1.setCurrentMinute(settings_m1);
    	
		TimePicker timepicker2 = (TimePicker) getActivity().findViewById(R.id.tp_time2);
		timepicker2.setIs24HourView(true);
    	timepicker2.setCurrentHour(settings_h2);
    	timepicker2.setCurrentMinute(settings_m2);
    	
    	SeekBar seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar_frequency);
    	seekBar.setMax(100);
    	seekBar.setProgress(setting_frequency);
    }
    
	@Override
	public void onPause()
	{
		super.onPause();

		SharedPreferences.Editor state = getActivity().getSharedPreferences(Constants.SETTING_PREFERENCE_NAME_KEY, 0).edit();
	
        TimePicker tp1 = (TimePicker)getActivity().findViewById(R.id.tp_time1);
    	TimePicker tp2 = (TimePicker)getActivity().findViewById(R.id.tp_time2);
       	SeekBar seekBar = (SeekBar)getActivity().findViewById(R.id.seekBar_frequency);
    	
    	state.putInt(Constants.SETTING_HOURS_FROM_KEY, tp1.getCurrentHour());
    	state.putInt(Constants.SETTING_MINUTES_FROM_KEY, tp1.getCurrentMinute());
    	state.putInt(Constants.SETTING_HOURS_TO_KEY, tp2.getCurrentHour());
    	state.putInt(Constants.SETTING_MINUTES_TO_KEY, tp2.getCurrentMinute());

    	state.putInt(Constants.SETTING_FREQUENCY_KEY, seekBar.getProgress() + 1); // Min period should be 1 minute
	    
	    state.commit();
	}
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}
